package com.caiyigo.baihua.fetion;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;

import com.caiyigo.baihua.entity.Xuesheng;
import com.caiyigo.baihua.fetion.service.DbServiceImpl;

/**
 * 2014-01-06
 * 
 * @author caihaihui
 * 
 */
public final class XueshengMap {

	public Map<String, Xuesheng> getXueshengMapKeyAtMobile() {
		if (xueshengMapKeyAtMobile.isEmpty()) {
			rebuildXueshengMapKeyAtMobile();
		}
		return xueshengMapKeyAtMobile;
	}

	public Map<String, Xuesheng> getXueshengMapKeyAtZkz() {
		if (xueshengMapKeyAtZkz.isEmpty()) {
			rebuildXueshengMapKeyAtZkz();
		}
		return xueshengMapKeyAtZkz;
	}

	public void rebuildXueshengMapKeyAtMobile() {
		if (xueshengMapKeyAtMobile.size() > 0) {
			xueshengMapKeyAtMobile.clear();
		}
		for (Iterator<Xuesheng> iterator = xueshengsList.iterator(); iterator
				.hasNext();) {
			Xuesheng xuesheng = iterator.next();
			xueshengMapKeyAtMobile.put(xuesheng.getXsSjOne(), xuesheng);
		}
	}

	public void rebuildXueshengMapKeyAtZkz() {
		if (xueshengMapKeyAtZkz.size() > 0) {
			xueshengMapKeyAtZkz.clear();
		}
		for (Iterator<Xuesheng> iterator = xueshengsList.iterator(); iterator
				.hasNext();) {
			Xuesheng xuesheng = iterator.next();
			xueshengMapKeyAtZkz.put(xuesheng.getXsZkz(), xuesheng);
		}
	}

	public List<Xuesheng> getXueshengList() {
		if (xueshengsList.isEmpty()) {
			rebuildXueshengList();
		}
		return xueshengsList;
	}

	public void rebuildXueshengList() {
		if (xueshengsList.size() > 0) {
			xueshengsList.clear();
		}
		xueshengsList.addAll(new FetionApplication().getDbService()
				.getXueshengsListFromDataBase(serviceId));
	}

	private static XueshengMap instance;

	public XueshengMap(String serviceId) {
		this.serviceId = serviceId;
	}

	public XueshengMap() {

	}

	public static XueshengMap getInstance(String serviceId) {
		if (instance == null) {
			instance = new XueshengMap(serviceId);
		}
		return instance;
	}

	public static XueshengMap getInstance() {
		if (instance == null) {
			instance = new XueshengMap();
		}
		return instance;
	}

	private String serviceId;
	private static Map<String, Xuesheng> xueshengMapKeyAtMobile = new HashMap<String, Xuesheng>();
	private static Map<String, Xuesheng> xueshengMapKeyAtZkz = new HashMap<String, Xuesheng>();
	private static List<Xuesheng> xueshengsList = new ArrayList<Xuesheng>();

}
